<?php


namespace Help\Controller;

use Think\Controller;

/**
 * Class IndexController
 * @package Help\Controller
 * @author:xjw129xjt xjt@ourstu.com
 */
class UsercenterController extends Controller
{

    protected $mUid = 0;
    protected $mHelpTypeModel = null;
    protected $mHelpModel = null;
    protected $mHelpAttendModel = null;
    protected $mHelpWorksModel = null;
    protected $mHelpSnapshotModel = null;
    protected $mHelpAttendSupportModel = null;
    protected $mHelpWorksSupportModel = null;
    protected $mHelpEvaluateModel = null;
    public function _initialize()
    {
        $aUid = I('request.uid',0,'intval');
        $this->mUid = $aUid ? $aUid :is_login();
        $this->assign('uid',$this->mUid);
        if(!$this->mUid){
           $this->error('请登录！');
        }
        $this->mHelpTypeModel =  D('HelpType');
        $this->mHelpModel = D('Help');
        $this->mHelpAttendModel = D('HelpAttend');
        $this->mHelpWorksModel = D('HelpWorks');
        $this->mHelpSnapshotModel = D('HelpSnapshot');
        $this->mHelpAttendSupportModel = D('help_attend_support');
        $this->mHelpWorksSupportModel = D('help_works_support');
        $this->mHelpEvaluateModel =    D('HelpEvaluate');
        $this->setTitle('帮忙');
    }

    public function publish($page = 1, $r = 5)
    {
        $map = array('uid' => $this->mUid, 'status' => 1);
        $list = $this->mHelpModel->getPage($map,$totalCount, $page, $r);
        $this->assign('totalCount', $totalCount);
        $this->assign('r', $r);
        $this->assign('list', $list);
        $this->assign('tabHash','help');
        $this->display();
    }

    public function attend($page = 1, $r = 50)
    {
        $map = array('uid' => $this->mUid, 'status' => 1);

        $list =  $this->mHelpAttendModel->getAttendHelpByUid($this->mUid, $page, $r);
        $totalCount =  $this->mHelpAttendModel->where($map)->count();
        $this->assign('totalCount', $totalCount);
        $this->assign('r', $r);
        $this->assign('list', $list);
        $this->assign('tabHash','help');
        $this->display();
    }

    public function delHelp()
    {
        $id = intval(I('post.id'));
        if (!is_login()) {
            $this->error('请登录后再操作');
        }
        if (!check_can_edit($id)) {
            $this->error('您没有权限进行该操作');
        }
        if (!help_is_exist($id)) {
            $this->error('该帮忙不存在');
        }

        $res = $this->mHelpModel->deleteHelp($id);
        if ($res) {
            clean_query_user_cache(is_login(),array('user_help_5','user_help_3'));
            $this->success('删除成功！', 'refresh');
        } else {
            $this->error('删除失败！');
        }
    }

    public function evaluate()
    {
        if(IS_POST){
            $data['help_id'] = $aHelpId = I('post.help_id', 0, 'intval');
            $data['content'] = $aContent = I('post.content', '', 'op_h');
            $data['attach_ids'] = $aAttach_ids = I('post.image', '', 'op_h');
            $data['type'] = $aType = I('post.type', 0, 'intval');
            $data['punctual'] = $aPunctual = I('post.punctual', 0, 'intval');
            $data['trustworthy'] = $aTrustworthy = I('post.trustworthy', 0, 'intval');
            $data['technique'] = $aTechnique = I('post.technique', 0, 'intval');
            $data['attitude'] = $aAttitude = I('post.attitude', 0, 'intval');
            $data['to_uid'] = $aToUid = I('post.to_uid', 0, 'intval');
            if (!is_login()) {
                $this->error('请登录后再评论');
            }

            if (empty($aHelpId)) {
                $this->error('帮忙不存在');
            }
            if (empty($aContent)) {
                $this->error('请填写内容');
            }
            if (empty($aType)) {
                $this->error('请选择好中差评');
            }
            if (empty($aPunctual)) {
                $this->error('请为守时打分');
            }
            if (empty($aTrustworthy)) {
                $this->error('请为守信打分');
            }
            if (empty($aTechnique)) {
                $this->error('请为技能打分');
            }
            if (empty($aAttitude)) {
                $this->error('请为态度打分');
            }
            if (check_is_evaluate($aHelpId)) {
                $this->error('您已经评论过了');
            }
            $help = $this->mHelpModel->getHelp($aHelpId);

            if(empty($help)){
                $this->error('帮忙不存在');
            }

            if($aToUid){
                if( !check_is_accept($aHelpId,$aToUid)){
                    $this->error('不能评论该用户');
                }
                if( check_publisher_is_evaluate($aHelpId,is_login(),$aToUid) || !check_is_my_publish($aHelpId)){
                    $this->error('不能评论该用户');
                }
            }

            !$data['to_uid'] && $data['to_uid'] = $help['uid'];
            $res = $this->mHelpEvaluateModel->addEvaluate($data);
            if ($res) {
                if($aType ==1){
                    M('help')->where(array('id'=>$aHelpId))->setInc('good_count');
                    M('member')->where(array('uid'=>$data['to_uid']))->setInc('credibility');

                    $good_count = D('HelpEvaluate')->where(array('to_uid'=>$data['to_uid'],'type'=>1))->count();
                    $evaluate_count = D('HelpEvaluate')->where(array('to_uid'=>$data['to_uid']))->count();
                    $rate =number_format($good_count/$evaluate_count*100,1);
                    M('member')->where(array('uid'=>$data['to_uid']))->setField('rate',$rate);
                    M('Help')->where(array('uid'=>$data['to_uid']))->setField('user_rate',$rate);
                }
                $this->mHelpSnapshotModel->addSnapshot($aHelpId,2);
                $this->success('评论成功！','refresh');
            } else {
                $this->error('评论失败！');
            }
        }else{
            $aId = I('get.id', 0, 'intval');
            $this->assign('help_id',$aId);
            $this->display();
        }

    }

    public function complain()
    {
        if (IS_POST) {
            $data['help_id'] = $aHelpId = I('post.help_id', 0, 'intval');
            $data['content'] = $aContent = I('post.content', '', 'op_h');
            $data['attach_ids'] = $aAttach_ids = I('post.image', '', 'op_h');
            if (!is_login()) {
                $this->error('请登录后再投诉');
            }
            if (empty($aHelpId) || !help_is_exist($aHelpId)) {
                $this->error('帮忙不存在');
            }

            if (empty($aContent)) {
                $this->error('请填写内容');
            }
            if (check_is_complain($aHelpId)) {
                $this->error('您已经投诉过了');
            }
            $res = D('HelpComplain')->addComplain($data);
            if ($res) {
                $this->success('投诉成功！','refresh');
            } else {
                $this->error('投诉失败！');
            }

        } else {
            $aId = I('get.id', 0, 'intval');
            $this->assign('help_id', $aId);
            $var['unid'] = substr(strtoupper(md5(uniqid(mt_rand(), true))), 0, 8);
            $var['fileSizeLimit'] = floor(8 * 1024) . 'KB';
            $this->assign($var);
            $this->display();
        }
    }

    public function askForHelp(){
        $data['help_id'] = $aHelpId = I('post.help_id',0,'intval');
        $data['to_uid'] = $aToUid = I('post.to_uid',0,'intval');
        $data['content'] = $aContent = I('post.content',0,'op_h');
        if(!is_login()){
            $this->error('请登录后再操作');
        }
        if (empty($aHelpId) || !help_is_exist($aHelpId)) {
            $this->error('帮忙不存在');
        }
        if (empty($aToUid) ) {
            $this->error('参数错误');
        }
        if(empty($aContent)){
            $this->error('请填写内容');
        }
        if( check_is_ask($aHelpId,is_login(),$aToUid) ){
            $this->error('该帮忙您已经请求对方帮忙');
        }

        $data['from_uid'] = is_login();
        $res =D('Help/HelpAsk')->addHelpAsk($data);
        if($res){
            $msg['to_uid']=$aToUid;
            $msg['from_uid']=is_login();
            $msg['to_help_id']=$aHelpId;
            $msg['content']='请求你去帮忙';
            $msg['title']='求帮忙';
            $msg['appName']='help';
            $msg['appType']='askForHelp';
            send_help_message($msg);
            $this->success('求帮忙成功！','refresh');
        }
        else{
            $this->error('求帮忙失败！');
        }
    }


   public function snapshot($page = 1, $r = 50)
   {
       $map = array('uid' => $this->mUid, 'status' => 1);
       $list =  $this->mHelpAttendModel->getAttendHelpByUid($this->mUid, $page, $r);
       $totalCount =  $this->mHelpAttendModel->where($map)->count();
       $this->assign('totalCount', $totalCount);
       $this->assign('r', $r);
       $this->assign('list', $list);
       $this->display();
   }



}